package com.yxjx.duoxue.datamodel;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.yxjx.duoxue.course.CourseListActivity;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.utils.UIHelper;

public class DMSortBy extends DMObject {
	private int sortById;
	private String sortByDesc;
	
	public int getSortById() {
		return sortById;
	}
	public void setSortById(int sortById) {
		this.sortById = sortById;
	}
	public String getSortByDesc() {
		return sortByDesc;
	}
	public void setSortByDesc(String sortByDesc) {
		this.sortByDesc = sortByDesc;
	}
	
	public static DMSortBy from(String str) {
		if (UIHelper.isEmpty(str)) {
			return null;
		}
		
		try {
			DMSortBy sortBy = new DMSortBy();
			JSONObject jo = new JSONObject(str);
			sortBy.setSortByDesc(castString(jo,"orderName"));
			sortBy.setSortById(castInteger(jo,"orderId"));
			return sortBy;
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 
	 * @param headTabSelected
	 * @param index
	 * @return
	 */
	public static String getTag(Context ctx, int headTabSelected, int index) {
		String def = "默认";
		if (headTabSelected == CourseListActivity.HEAD_TAB_COURSE) {
			ArrayList<DMSortBy> sortBys = Globals.getInstance(ctx).getSortBysCourse();
			if (sortBys == null) {
				return def;
			} else if (index < sortBys.size()) {
				return sortBys.get(index).getSortByDesc();
			} else {
				return sortBys.get(0).getSortByDesc();
			}
		} else if (headTabSelected == CourseListActivity.HEAD_TAB_SCHOOL) {
			ArrayList<DMSortBy> sortBys = Globals.getInstance(ctx).getSortBysSchool();
			if (sortBys == null) {
				return def;
			} else if (index < sortBys.size()) {
				return sortBys.get(index).getSortByDesc();
			} else {
				return sortBys.get(0).getSortByDesc();
			}
		}
		return def;
	}
	
	public static int getIndex() {
		return 0;
	}
}
